package top.lshaci.learning.springboot.web.config;

import cn.hutool.core.util.StrUtil;
import org.springframework.core.convert.converter.Converter;
import top.lshaci.learning.springboot.web.model.Pet;

/**
 * String2PetConverter
 *
 * @author lshaci
 * @since 1.0.0
 */
public class String2PetConverter implements Converter<String, Pet> {

    @Override
    public Pet convert(String source) {
        if (StrUtil.isBlank(source)) {
            return null;
        }
        Pet pet = new Pet();
        String[] split = source.split(",");
        pet.setName(split[0]);
        pet.setAge(Integer.parseInt(split[1]));
        return pet;
    }
}
